#!/usr/bin/env expect
############################################################################
# Purpose:  Test of priority multifactor algo to get correct decay and
#           such.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
# Note:    This script generates and then deletes files in the working directory
#          named test24.1.prog
############################################################################
# Copyright (C) 2009 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id     "24.1"
set exit_code   0
set test_prog   "test$test_id.prog"
set matches     0

print_header $test_id

set damp [get_fs_damping_factor]
if {$damp != 1} {
	send_user "Unable to run with FairShareDampeningFactor=$damp (must be 1)\n"
	exit 0
}

#
# Delete left-over programs and rebuild them
#
file delete $test_prog

send_user "build_dir is $build_dir\n"
send_user "$bin_cc ${test_prog}.c -g -pthread -o ${test_prog} -I${build_dir} -I${src_dir} ${build_dir}/src/api/libslurm.o ${build_dir}/src/slurmctld/locks.o ${build_dir}/src/sshare/process.o -ldl -lm  -export-dynamic -lresolv\n"
exec       $bin_cc ${test_prog}.c -g -pthread -o ${test_prog} -I${build_dir} -I${src_dir} ${build_dir}/src/api/libslurm.o ${build_dir}/src/slurmctld/locks.o ${build_dir}/src/sshare/process.o -ldl -lm  -export-dynamic -lresolv
exec $bin_chmod 700 $test_prog

# Usage: test24.1.prog
spawn ./$test_prog
expect {
	"No last decay" {
		send_user "This error is expected.  No worries.\n"
		exp_continue
	}
	"error: Can't save decay state" {
		send_user "This error is expected.  No worries.\n"
		exp_continue
	}
	"AccountA||40|0.400000|45|0.450000|0.450000|0.458502|" {
		incr matches
		exp_continue
	}
	"AccountB||30|0.300000|20|0.200000|0.387500|0.408479|" {
		incr matches
		exp_continue
	}
	"AccountB|User1|1|0.300000|20|0.200000|0.387500|0.408479|" {
		incr matches
		exp_continue
	}
	"AccountC||10|0.100000|25|0.250000|0.300000|0.125000|" {
		incr matches
		exp_continue
	}
	"AccountC|User2|1|0.050000|25|0.250000|0.275000|0.022097|" {
		incr matches
		exp_continue
	}
	"AccountC|User3|1|0.050000|0|0.000000|0.150000|0.125000|" {
		incr matches
		exp_continue
	}
	"AccountD||60|0.600000|25|0.250000|0.250000|0.749154|" {
		incr matches
		exp_continue
	}
	"AccountE||25|0.250000|25|0.250000|0.250000|0.500000|" {
		incr matches
		exp_continue
	}
	"AccountE|User4|1|0.250000|25|0.250000|0.250000|0.500000|" {
		incr matches
		exp_continue
	}
	"AccountF||35|0.350000|0|0.000000|0.145833|0.749154|" {
		incr matches
		exp_continue
	}
	"AccountF|User5|1|0.350000|0|0.000000|0.145833|0.749154|" {
		incr matches
		exp_continue
	}
	"AccountG||0|0.000000|30|0.300000|0.300000|0.000000|" {
		incr matches
		exp_continue
	}
	"AccountG|User6|0|" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: spawn IO not responding\n"
		cancel_job $job_id
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 13} {
	send_user "\nFAILURE: we didn't get the correct priorities from the plugin ($matches != 13)\n"
	set exit_code 1
}

if {$exit_code == 0} {
	file delete $test_prog
	send_user "\nSUCCESS\n"
}
exit $exit_code
